/**
 * 
 */
package com.genITeam.ria.actions;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.genITeam.ria.controller.AbstractController;

public abstract class AbstractAction {
	protected HttpServletRequest request;

	protected HttpServletResponse responce;

	protected ServletContext context;

	protected AbstractController controller;

	public void init(AbstractController c, ServletContext thecontext,
			HttpServletRequest thereq, HttpServletResponse theres) {
		this.controller = c;
		this.context = thecontext;
		this.request = thereq;
		this.responce = theres;
	}

	public void initAction(HttpServletRequest thereq, HttpServletResponse theres) {
		this.request = thereq;
		this.responce = theres;
	}

	// abstract public void execute() throws Exception;

	/**
	 * reads a parameter from the request
	 * 
	 * @param paramName
	 *            the parameter name to read
	 * @return the value of paramName
	 */

	public String getParameter(String paramName) {
		return request.getParameter(paramName);
	}

	/**
	 * reads a parameter from the request, uses default if not found
	 * 
	 * @param paramName
	 *            the parameter name to read
	 * @param defaults
	 *            default value if param not set
	 * @return the value of paramName
	 */
	public String getParameter(String paramName, String defaults) {
		String o = request.getParameter(paramName);
		if (o != null)
			return o;
		else
			return defaults;
	}

	/**
	 * 
	 * @return true if action requires authentication
	 */
	// abstract public boolean requiresAuthentication();
	/**
	 * http forwards to the specified context path
	 * 
	 * @param path
	 * @throws ServletException
	 * @throws IOException
	 */
	public void forwardTo(String path) throws ServletException, IOException {
		// if (Log.isTracing)
		// {
		// Log.info("AbstractAction", "forwardTo " + path);
		// }
		RequestDispatcher requestDispatcher = request
				.getRequestDispatcher(path);
		requestDispatcher.forward(request, responce);
	}

	/**
	 * Writes the result string in httpresponse object. Did not throw the
	 * exception , as the user might not have any way to recover out of this.
	 * 
	 * @param response
	 *            the string to be written
	 * @return true if the function executes successfully else the function
	 *         returns false
	 */
	public boolean writeResponse(String response) {
		boolean success = true;
		try {
			PrintWriter out = this.responce.getWriter();
			out.write(response);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
			success = false;
		}
		return success;
	}

	// need to define XML in constants
	protected String getXMLInput() {
		String xml = this.request.getParameter("xml");
		// System.out.println(xml);
		return xml;
	}
}
